Databases
Learning Objectives : Student should be able to -
Q1. What is Database ?
⇒ A database is a computerized record-keeping system.
⇒ It is an organized collection of data, that allows people to quickly search and extract information in a way that meets their need; edit and delete records using computer software.
⇒ The data can include text, numbers, pictures or anything that can be stored in a computer.
Q2. Describe the following three types of database.
1) Single-table database (or) Flat database.
⇒ A Single-table database is a simple database that stores all data in a single table in rows and columns, also called as Flat database.
⇒ It contains unrelated tables like spreadsheet. It can be converted into text file separating each field using delimiters like comma, hyphen etc.
2) Relational database.
⇒ A computerized record-keeping system that contains two or more tables of data, interrelated through key fields called relationships.
⇒ Relational database are very flexible for accessing, managing, and updating data.
3) Distributed database.
⇒ Distributed database is relational database distributed over a computer network allowing multiple users to access data simultaneously.
Q3. State the advantages and disadvantages of using database.
⇒ Avoid data redundancy (duplication of data).
⇒ Increased data consistency (or correctness), allows only valid data defined by some rules.
⇒ Improve data access to users through use of host and query languages.
⇒ Improved data security to access the data through user credentials.
⇒ A database can be shared with number of users over the network.
⇒ Database systems are complex, difficult, and time-consuming to design.
⇒ Extensive conversion costs in moving from a file-based system to a database system.
⇒ Initial training required for all programmers and users.
Q4. Give three uses of database.
Q5. a) What is (DBMS) Database Management System.
⇒ A database management system is a set of programs that allows the creation of data files called tables and permits the ways to process, alter and extract records.
It provides the following features :
- Allows the data to be stored by creating different tables which could be linked together.
- Allows to search records which meet certain criteria (conditions) using Query.
- Allows editing and deletion of records.
- Allows the alteration of structure of the table.
- Allows import and export of data.
- Allows to set security to access the data.
b) Describe the following four main components of database management system.
✬ Table : A Table is a data structure, a basic unit of storing data in a database. It stores related data in rows called records with different piece of information in columns called fields of a record.
✬ Form : A Form is a database object, created for the user to enter, edit or display data from the data source table. It provides a user friendly interface for a database application.
✬ Query : A Query is a request used to extract data from a database according to the user’s request. It uses structured query language (SQL) that can be written or designed through query design grids.
✬ Report : A Report is an database object used to display and print formatted data in an organized manner form the database table.
Q6. a) What is meant by Record and Field ?
⇒ Records is a basic data structure composed of different fields in a rows. Each record is a single row in the table that contains different information of an item. A set of records makes a database table.
⇒ Field refers to the columns, or data categories of same data type used by each record. It represents an attribute (a characteristic or quality) of some entity (like object, person, place, or event).
>
b) Describe Primary Key-field .
⇒ A primary key is a field in a table which uniquely identifies each record in a database table.
⇒ Primary keys must contain unique values.
⇒ A primary key column cannot have NULL values or left empty.
c) Describe two purpose of primary key-field.
- It serves as a unique identifier for a row of data in a database table.
- It stops the possibility of duplicate data.
- It helps to speeds up queries, searches and sort requests.
- It is used to establish relationship with other tables by linking the fields.
Q7. Describe how to create or design a database Table.
- Organize data of each individual or thing in rows, with each column containing data of same data type.
- In table design view, name each field with its appropriate data type.
- Add validation check by specifying proper attributes like size (length of field), format, validation rule etc.
- Identify the field which uniquely identifies each record and specify it as primary key field.
- Save the table structure to store data later using datasheet view or form.
Q8. Describe the following Fields attributes (or properties) that need to be decided before setting up the database to control how they receive data.
Field name : | The names you give to the columns in a table to identify the field, so it is mandatory. |
Field / Data type : | Determines the type of data that a user can enter into the field. It validates and restricts invalid data types. Based on data-type other properties of the fileds are provided. |
Field size : | Specifies the required length or size of data. It helps to minimize the wastage of storage space. |
Format : | Specifies a unique format of data to be allowed and stored in a uniform way. |
Input Mask : | It is a string of characters that indicates the format of valid input values. |
Validation Rule : | Specifies the criteria or rule that should be met before storing data in the field. |
Q9. a) Describe different data types used in designing a table.
Data type | Description | Example |
Text | It allows alphanumeric data, like text, numbers and symbols. | Name : James Address : 73, High Street. Postal code : CV34 5TR |
Character | It allows only a single character of any alphanumeric data. | Code : N / C / P . . . House : R / B / G / Y . . . Category : D / W / V . . . |
Integer | It allows only a positive or negative whole numbers only. | Age : 16 Quantity : 36 Temperature : - 10 |
Real | It allows only a positive or negative decimal numbers. | Lenth (cm) : 12.5 Weight (kg) : 52.36 Temperature : - 2.5 |
Currency | It allows price with currency symbols and ensures there are two decimal places. | Cost : $5.75 Amount : £468.25 Price : ₹18.75 |
Date/Time | It checks and allows only date and time in an appropriate formate. | Long Date : 20 February 2006 18:21:35 Medium Date : 20-Feb-06 06:21 PM Short Date : 20/02/06 18:21 |
Boolean | It allows only any one of the two choices. | Paid : Yes / No Option : True / False Gender : Male / Female |
b) Give two reasons why choosing the correct data-type is important.
- Do validation check over data while entering to prevent invalid characters being stored.
- To check the correct format of data, like date and time to be stored in uniform way.
c) Give two reasons why setting the field-size or maximum length of a field is important.
- File size can be kept as small as possible.
- Time taken to process data can be kept minimum.
Q10. Codes are used to represent information and are useful, provided we know how to interpret them.
a) Why Coding data is useful?
⇒ Codes are quicker to type in.
⇒ Using codes reduces the size of the files.
⇒ Increases the speed of search and process performed on the data.
⇒ Codes are often unique and are easy to search.
b) Give three important factors that should be taken into account when designing codes.
- Code should always be of same length.
- Codes should contain enough details and should not be too short.
- Codes should be easy to use.
Q11. a) Describe what is meant by Data Validation .
⇒ Validation is an automatic computer check to ensure that the data entered is sensible, reasonable and consistent. Data is accepted only if it falls withing the set rules. It does not check the accuracy of data.
b) Describe how the following Validation-check works with example of its usage.
Validation type | How it works | Example usage |
Presence check | Checks that data has been entered into a field. | Field which cannot be left blank like primary key field of a database. |
Data-type check | Ensure that the correct type of data (like text, number, date) is entered into that field. | Name (Text), Date of birth (Date), Age (Number), Price (Currency). |
Length check | Checks the data isn't too short or too long. | A password which needs to be six letters long. |
Format check | Checks the data is in the right format. | Passport number is in the form M5228907 where M is a letter followed by 7 digits. |
Date format check | Checks the date is in the right format. | Date of birth is in the form 20 Jan. 1998 or 20/01/1998 |
Check digit | The last one digit in a code (number) is used to check the other digits are correct. | Bar code use check digits at right most of the number to check its integrity. |
List check (or) Lookup table | Looks up acceptable values in a list or table. | There are only seven possible days of the week. |
Range check | Checks that a value falls within the specified range of values. | Number of hours worked must be less than 50 and more than 0. |
Consistency (or) Cross-field check | Checks that different fields in the same record correspond correctly. | If ‘Mr’ has been entered into the title field, ‘Male’ must be entered into the gender field. |
Spell check | Looks up words in a dictionary. | When word processing. |
Q12. a) What is meant by SQL query in database ?
⇒ SQL stands for Structured Query Language. It is a standard query language for writing scripts, to make request to the database for the records which meets the criteria we specify.
⇒ An SQL script is a list of SQL commands that performs a given task, often stored in a file so the script can be reused.
b) State the benefits of using a query.
c) Explain the function of each of the following SQL statements.
Q13. Describe how to create SQL query to retrieve data from database table.
Examples of criteria or condition to specify in WHERE command line :
Criteria | Query result |
= "Male" // or // Like "Male" |
Returns records where the field exactly match the value Male. |
Not "Apple" // or // Not Like "Apple" |
Returns records where the field do not match the value Apple. |
Like "U * " | Returns records where the field value (text) begins with the string "U", such as UK, USA and so on. Asterisk sign (*) is a "wildcard" that denotes any number of characters including blank-space. |
Not Like "U * " | Returns records where the field value (text) do not begin with the string "U". |
"Apple" OR "Orange" | Returns records where the field value (text) match one of two values, like records with both "Apple" and "Orange". |
100 // or // = 100 |
Returns records where the field value is equal to 100. |
Not 100 // or // < > 100 |
Returns records where the field value is not equal to 100. |
> 50 | Returns records where the field value is greater than 50. |
>= 50 | Returns records where the field value is greater than or equal to 50. |
< 75 | Returns records where the field value is less than 75. |
<= 75 | Returns records where the field value is less than or equal to 75. |
20 OR 25 | Returns records where the field value is either 20 or 25. |
>0 AND <50 // or // Between 0 and 50 |
Returns records where the field value is between 0 and 50 (within a range of values). |
< 50 OR > 100 | Returns records where the field value is outside the range of values 50 and 100. |
Is Null | Returns records where there is no value in the field. |
Is Not Null | Returns records where the value is not missing in the field. |
" " (a pair of quotes) |
Returns records where the field is set to blank (but not null). |
Not " " | Returns records where the field has a nonblank value. |
" " Or Is Null | Returns records where there is either no value in the field, or the field is set to a blank value. |
► Exam-style Questions
Q14. A database table, MARKS, is created with fields StdID, StdName, Sex, DOB, Grade, Eng, Dhi and Math to store student's details.
StdID | StdName | Sex | DOB | Grade | House | Eng | Dhi | Math |
2030 | ATHOOF | M | 07-May-04 | 10(B) | Red | 70 | 82 | 94 |
2031 | FAINAAN | M | 12-Apr-04 | 10(A) | Yellow | 68 | 77 | 86 |
2033 | IYALA | F | 10-Oct-04 | 10(A) | Green | 89 | 92 | 95 |
2036 | MAIHA | F | 13-Sep-04 | 10(A) | Red | 76 | 82 | 90 |
2038 | SHAAIL | M | 08-Oct-04 | 10(B) | Orange | 54 | 79 | 58 |
2039 | HASEEN | M | 14-Dec-04 | 10(B) | Blue | 46 | 65 | 49 |
2043 | EENAASH | F | 10-Oct-04 | 10(A) | Green | 57 | 67 | 62 |
2238 | MAZEENA | F | 16-May-04 | 10(A) | Orange | 49 | 62 | 70 |
2053 | MAISHA | F | 24-Jun-04 | 10(B) | Yellow | 62 | 74 | 75 |
2072 | DHAAEE | M | 31-Dec-04 | 10(B) | Blue | 50 | 59 | 88 |
a) (i) State how many records and fields are there in the above table.
(ii) State which field you would choose for the primary key : StdID
Give reason of your choice : The field "StdID" contains unique data elements, which could be used to identify each record (all data elements are different).
b) State what data type you would choose for the following fields.
StdID | : | Integer |
StdName | : | Text |
Sex | : | Boolean (M/F) |
DOB | : | Date/Time |
Grade | : | Text |
Eng | : | Integer |
c) Write the SQL statement to display student's index, name and sex in ascending order of their name, grouped by sex listing male first female next, where marks scored in english is greater than 60.
FROM MARKS
(Note : The field which has to be grouped should be sorted first.)
What would be output of the SQL query you have written above ?
2030 | M | ATHOOF |
2031 | M | FAINAAN |
2033 | F | IYALA |
2036 | F | MAIHA |
2053 | F | MAISHA |
d) Write the SQL statement to display the name and marks scored in math subject of all girls of grade 10(A) in descending order of their marks in maths.
FROM MARKS
WHERE Sex Like "F" AND Grade Like "10(A)"
ORDER BY Math DESC ;
e) Write the SQL statement to display student's index, name and grade in descending order of their marks in dhivehi grouped by grade of all students, who has scored marks less than 45 and greater than or equal to 60 in dhivehi.
FROM MARKS
WHERE Dhi < 45 OR Dhi >= 60
f) Write the SQL statement to display student's index, name and sex in descending order of their marks in english, grouped by their gender female first, male next, who has scored marks between 60 and 75 (inclusive) in english.
FROM MARKS
WHERE Eng >= 60 AND Eng <= 75
(Note : Since criteria "WHERE Eng >= 60 AND Eng <= 75" includes the boundary values like 60, 61, 62,...75, you can also use the criteria, like WHERE Between 60 and 75 which will return the same records.)
g) Write the SQL statement to display student's name only in descending order of marks in english, grouped by their grade, who has scored either marks greater than or equal to 50 in english or marks greater than 60 in dhivehi.
SELECT StdName
FROM MARKS
WHERE Eng >= 50 OR Dhi > 60
h) Write the SQL statement to display student's name and grade, who has passed in all their three subjects with marks greater than or equal to 60.
SELECT StdName
FROM MARKS
i) Write the SQL statement to display student's name and grade, who has scored marks greater than or equal to 85 in maths and marks greater than or equal to 60 in either english or dhivehi.
SELECT StdName
FROM MARKS
j) Write SQL statement to display student's index, name, grade, who belongs to the house other than Orange (like, Red, Green, Blue or Yellow).
SELECT StdID, StdName, Grade
FROM MARKS
WHERE House Not Like "Orange" ;
k) Write the SQL statement to display student's name and grade, who belongs to either Red or Blue house only.
SELECT StdID, StdName, Grade
FROM MARKS
l) Write the SQL statement to return the name, grade and data of birth (DOB) of students who were born between 1st June 2004 and 30th October 2004.
SELECT StdName, Grade, DOB
FROM MARKS
WHERE DOB >= #6/1/2004# AND DOB <= #10/30/2004# ;
// OR //
m) Write the SQL statement to return the name, sex and total mark of english, dhivehi and math subject scored by each student of grade 10(A), listed in highest to lowest order of their total marks.
SELECT StdName, Sex, (Eng + Dhi + Math) AS TotalMark
FROM MARKS
WHERE Grade Like "10(A)"
ORDER BY (Eng + Dhi + Math) DESC ;
Q15. A database table, SALES, is used to keep a record of items made and sold by a furniture maker.
ItemNo | OrderNo | Notes | Qty | Amount | Status |
CH001 | 1921 | Smith – six dining chairs | 6 | 4500 | Delivered |
TB003 | 1921 | Smith – large table | 2 | 3200 | In progress |
CH001 | 1924 | Hue - extra chairs | 4 | 3800 | Not started |
CH003 | 1925 | Easy chairs | 2 | 3600 | Cancelled |
BN001 | 1927 | Patel - replacement bence | 1 | 1350 | Not started |
ST002 | 1931 | Sola - small table | 1 | 2400 | Delivered |
CH003 | 1927 | Patel - eight dining chairs with arms | 8 | 9600 | In progress |
TB003 | 1927 | Pagel - large table | 1 | 3450 | Not started |
a) Explain why the field Item number could not be used as a primary key.
⇒ All the fields contains repeated data element, so no field can uniquely identify each record and cannot be set as primary key field.
b) Write the SQL statement to calculate and output the amount of money collected from delivered items.
SELECT SUM(Amount) AS TotalAmt
FROM SALES
WHERE Status Like "Delivered" ;
What would be the output by the above SQL query ?
TotalAmt
6900
c) Write the SQL statement to count and output how many of the items are in progress.
SELECT COUNT(Status) AS NoItems
FROM SALES
WHERE Status Like "In progress" ;
What would be the output by the above SQL query ?
NoItems
2
d) Write the SQL statement to count and output the number of items has collected amount more than 3500.
SELECT COUNT(Amount) AS NoItems
FROM SALES
WHERE Amount > 3500 ;
What would be the output by the above SQL query ?
NoItems
4
Q16. SQL statement WHERE which specifies the condition often include values from fields, these values need to be stated in a form that matches the data type for the field.
Tick(✔) the appropriate column to show how to provide values in criteria for a field to filter out the records.
Field data type | How to provide value | |
Value should be enclosed in single or double quotation mark. |
Value should not be enclosed in single or double quotation mark. |
|
Text | ✔ | |
Integer | ✔ | |
Real | ✔ | |
Boolean | ✔ | |
Character | ✔ | |
Date/Time | ✔ |
Note : Data type Date/Time should be enclosed in hash (#) not single or double quote.
Data type Boolean store data using binary digits 0 and 1, so it should not be enclosed in single or double quote.
* * * * * * * * *
* * * * * *
* * *
*